export default{
	// 保存数据状态
	state: {
		cart: [],
		cartPay: [],
		cartPayDetail: [],
	},
	// 过滤数据
	getters:{
		// 获取支付成功的商品
		getPaySuccessData(state){
			let paySuccessData = []
			let allData = [...state.cartPayDetail,...state.cartPay]
			if(allData.length !== 0){
				allData.map(item=>{
					if(item.payStatus == true){
						paySuccessData.push(item)
					}
				})
			}
			return paySuccessData
		},
		// 获取待支付商品
		getPayWaitData(state){
			let payWaitData = []
			let allData = [...state.cartPayDetail,...state.cartPay]
			if(allData.length !== 0){
				allData.map(item=>{
					if(item.payStatus == false){
						payWaitData.push(item)
					}
				})
			}
			return payWaitData
		},
		// 获取全部订单
		getPayAllData(state){
			let allData = [...state.cartPayDetail,...state.cartPay]
			if(allData.length !== 0){
				return allData
			}
		},
		// 获取购物车商品数量
		getCartLength(state){
			return state.cart.length
		},
		// 获取购物车商品选中状态长度
		getCartStatus(state){
			let statusLength = 0;
			state.cart.map(item=>{
				item.status == true ? statusLength++ : ''
			})
			return statusLength
		},
		// 获取商品总价
		getTotalPrice(state){
			let totalPrice = 0
			state.cart.map(item=>{
				if(item.status == true){
					totalPrice += item.price * item.buyNum
				}
			})
			return totalPrice
		},
	},
	// 修改数据
	mutations:{
		// 加入购物车
		addCartItem(state,item){
			if(state.cart.length == 0){
				state.cart.push(item)
			}else{
				state.cart.map(items=>{
					if(items.name == item.name){
						items.buyNum++
					}else{
						state.cart.push(item)
						let arr = new Set(state.cart)
						state.cart = Array.from(arr)
					}
				})
			};
			uni.showToast({
			    title: '已加入购物车',
			    duration: 1000
			});
		},
		// 减少商品数量
		reduceNum(state,index){
			let cartData = state.cart
			cartData[index].buyNum > 1 ? cartData[index].buyNum-- : ''
		},
		// 增加商品数量
		addNum(state,index){
			let cartData = state.cart
			100 > cartData[index].buyNum >= 1 ? cartData[index].buyNum++ : ''
		},
		// 删除单个商品
		delItem(state,index){
			state.cart.splice(index,1)
		},
		// 改变每个商品状态
		changeItemStatus(state,index){
			let cartData = state.cart
			cartData[index].status = !cartData[index].status
		},
		// 改变所有商品选中状态
		changeAllStatus(state,statusLength){
			if(state.cart.length == statusLength){
				state.cart.map(item=>{
					item.status = false
				})
			}else{
				state.cart.map(item=>{
					item.status = true;
				})
			}
		},
		// 删除选中商品
		checkedStatus(state,statusLength){
			if(state.cart.length == statusLength){
				uni.showModal({
					title: '删除提示',
					content: '此操作将清空购物车？',
					success: function (res) {
						state.cart = []
					}
				});
			}else{
				uni.showModal({
					title: '删除提示',
					content: '确定删除选中商品？',
					success: function (res) {
						let newCart = []
						state.cart.filter(item=>{
							if(item.status == false){
								newCart.push(item)
							}
						})
						state.cart = newCart
					}
				});
			}
		},
		// 去支付选中商品
		payCheckedStatus(state,statusLength){
			let cartData = state.cart
			if(cartData.length == statusLength){
				state.cartPay = cartData
			}else{
				let checkedData = []
				cartData.map((item,index)=>{
					if(item.status == true){
						checkedData.push(item)
					}
				})
				state.cartPay = checkedData
			}
		},
		// 去支付详情页商品
		payDetail(state,detailData){
			state.cartPayDetail.push(detailData)
		}
	},
	// 异步操作数据
	// actions,
}
